<template>
  <div class="app-container">
    <el-card style="position: relative">
      <RouterHeader :title="title()"></RouterHeader>
      <el-form
        :model="form"
        ref="ruleFormRef"
        label-width="140px"
        label-position="right"
      >
        <el-row :gutter="20" class="form-single-info">
          <el-col :span="20">
            <div class="title"><h5>基本信息</h5></div>
            <el-row>
              <el-col :span="12">
                <el-form-item label="项目名称" prop="projectName">
                  <el-input
                    class="input-item"
                    v-model="form.projectName"
                    placeholder="请输入"
                    :disabled="true"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="业主单位" prop="enterpriseName">
                  <el-input
                    class="input-item"
                    v-model="form.enterpriseName"
                    placeholder="请输入"
                    :disabled="true"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="项目联系人电话" prop="enterprisePhone">
                  <el-input
                    class="input-item"
                    v-model="form.enterprisePhone"
                    placeholder="请输入"
                    :disabled="true"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="项目联系人" prop="enterprisePerson">
                  <el-input
                    class="input-item"
                    v-model="form.enterprisePerson"
                    placeholder="请输入"
                    :disabled="true"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="行业" prop="industry">
                  <el-select
                    v-model="form.industry"
                    multiple
                    filterable
                    allow-create
                    default-first-option
                    :reserve-keyword="false"
                    placeholder="请输入"
                    disabled
                  >
                    <el-option
                      v-for="item in industryDownOption"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="所在地区" prop="addressList">
                  <el-cascader
                    v-model="form.addressList"
                    ref="ref_areaCodes"
                    :options="areaList"
                    :props="areaCodeprops"
                    :show-all-levels="true"
                    clearable
                    :disabled="true"
                  ></el-cascader>
                </el-form-item>
              </el-col>
              
              <!-- <el-col :span="12">
                <el-form-item label="项目属性" prop="projectProperty">
                  <el-input
                    class="input-item"
                    v-model="form.projectProperty"
                    placeholder="请输入"
                    :disabled="true"
                  ></el-input>
                </el-form-item>
              </el-col> -->
              <el-col :span="12">
                <el-form-item label="审批部门" prop="approvalDept">
                  <!-- <el-input
                    class="input-item"
                    v-model="form.approvalDept"
                    placeholder="请输入"
                    :disabled="true"
                  ></el-input> -->
                  <el-select
                    v-model="form.approvalDept"
                    multiple
                    filterable
                    allow-create
                    default-first-option
                    :reserve-keyword="false"
                    placeholder="请输入"
                    :disabled="true"
                  >
                    <el-option
                      v-for="item in approvalDeptOptions"
                      :key="item.id"
                      :label="item.label"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="监督部门" prop="superviseDept">
                  <el-select
                    class="input-item"
                    v-model="form.superviseDept"
                    multiple
                    filterable
                    allow-create
                    default-first-option
                    :reserve-keyword="false"
                    placeholder="请选择"
                    :disabled="true"
                  >
                    <el-option
                      v-for="item in superviseDeptOptions"
                      :key="item.id"
                      :label="item.label"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="业务类型" prop="businessTypeList">
                  <el-cascader
                    v-model="form.businessTypeList"
                    ref="ref_business"
                    :options="businesstypeTree"
                    :props="businesstProps"
                    :show-all-levels="true"
                    clearable
                    @change="businessChange"
                    :disabled="true"
                  ></el-cascader>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="中标金额">
                  <el-input
                    class="input-item"
                    v-model="formInline.getmoney"
                    placeholder="请输入"
                    :disabled="isDisabled()"
                  >
                    <template #append> 万元 </template>
                  </el-input>
                </el-form-item>
              </el-col>
              <!-- <el-col :span="24">
                <el-form-item label="保密协议" prop="secrecyAgreement">
                  <el-radio-group
                    v-model="form.secrecyAgreement"
                    :disabled="true"
                  >
                    <el-radio :label="0">否</el-radio>
                    <el-radio :label="1">是</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col> -->
              <!-- <el-col :span="24" v-if="form.secrecyAgreement === 1">
                <el-form-item
                  label="上传保密协议"
                  prop="secrecyAgreementFileList"
                >
                  <fileupload
                    @update:modelValue="handleModelValueUpdate"
                    :modelValue="form.secrecyAgreementFileList"
                    :flag="true"
                  ></fileupload>
                </el-form-item>
              </el-col> -->
              <!-- <el-col :span="24">
                <el-form-item label="投标项目" prop="tenderProject">
                  <el-radio-group v-model="form.tenderProject" :disabled="true">
                    <el-radio :label="0">否</el-radio>
                    <el-radio :label="1">是</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col> -->
              <!-- <el-col :span="24">
                <el-form-item label="优先级" prop="priority">
                  <el-select
                    v-model="form.priority"
                    clearable
                    placeholder="请选择"
                    style="width: 240px"
                    @change="changePriority"
                    :disabled="true"
                  >
                    <el-option
                      v-for="item in priorityOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col> -->
              <el-col :span="24">
                <el-form-item label="项目简述" prop="projectBriefly">
                  <el-input
                    class="input-item"
                    type="textarea"
                    :autosize="{ minRows: 2 }"
                    show-word-limit
                    v-model="form.projectBriefly"
                    placeholder="请输入"
                    :disabled="true"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="项目备注" prop="projectRemark">
                  <el-input
                    class="input-item"
                    type="textarea"
                    :autosize="{ minRows: 2 }"
                    show-word-limit
                    v-model="form.projectRemark"
                    placeholder="请输入"
                    :disabled="true"
                  ></el-input>
                </el-form-item>
              </el-col>
              <!-- <el-col :span="24">
                <el-form-item label="添加附件" prop="projectAnnexList">
                  <fileupload
                    @update:modelValue="handleModelValueFile"
                    :modelValue="form.projectAnnexList"
                    :flag="true"
                  ></fileupload>
                </el-form-item>
              </el-col> -->
            </el-row>
            <div class="table-content">
              <el-col :span="24">
                <div class="title"><h5>文件信息</h5></div>
                <el-table
                  :data="formInline.taskList"
                  style="width: 100%; margin-top: 10px"
                >
                  <el-table-column type="index" label="序号" width="50" />
                  <el-table-column prop="taskName" label="文件/任务名称" />
                  <el-table-column label="附件">
                    <template #default="scope">
                      <el-tag @click="handleDetailFile(scope.row)">查看</el-tag>
                    </template>
                  </el-table-column>
                  <el-table-column prop="createPerson" label="创建人" />
                  <el-table-column prop="taskCreateTime" label="创建时间" />
                </el-table>
                <div>
                  <div class="title"><h5>文件补充/图片</h5></div>
                  <el-row>
                    <el-col :span="12">
                      <!-- <el-form-item label="上传">        :flag="route.query.name === '6' ? true : false" -->
                      <fileupload v-if="ist == '3'"
                        :isShowTip="route.query.name === '3' ? false : true"
                        :flag="route.query.name === '6'"
                        :modelValue="formInline.documentSupplementList"
                        @update:modelValue="handleModelValueUpdate"
                      ></fileupload>
                      
                      <indexcategory v-else-if="ist == '1'"
                        :isShowTip="route.query.name === '3' ? false : true"
                        :flag="route.query.name === '6'"
                        :modelValue="formInline.documentSupplementList"
                        @update:modelValue="handleModelValueUpdate">
                      </indexcategory>

                      <indexcategorysecond v-else-if="ist == '2'"
                        :isShowTip="route.query.name === '3' ? false : true"
                        :flag="route.query.name === '6'"
                        :modelValue="formInline.documentSupplementList"
                        @update:modelValue="handleModelValueUpdate">
                      </indexcategorysecond>
                      <!-- </el-form-item> -->
                    </el-col>
                  </el-row>
                </div>
                <div v-if="route.query.name !== '1'">
                  <div class="title"><h5>项目审批进度</h5></div>
                  <el-timeline style="max-width: 600px">
                    <el-timeline-item
                      v-for="(activity, index) in activities"
                      :key="index"
                      :color="activity.color"
                      :timestamp="activity.updateTime"
                    >
                      <div style="display: flex; align-items: center">
                        <img :src="url + activity.avatar" class="user-avatar" />
                        <span
                          style="
                            font-size: 14px;
                            font-weight: 500;
                            margin-left: 5px;
                          "
                        >
                          {{ activity.username }} ({{
                            activity.checkHisResult === "2"
                              ? "未审核"
                              : "已审核"
                          }})
                          {{
                            activity.zuofei === "0"
                              ? " _作废"
                              : activity.zuofei === "null"
                              ? ""
                              : ""
                          }}
                          :</span
                        >
                        <span
                          style="
                            font-size: 14px;
                            font-weight: 400;
                            margin-left: 5px;
                          "
                        >
                          {{ activity.opinion }}</span
                        >
                      </div>
                    </el-timeline-item>
                  </el-timeline>
                </div>

                <div
                  v-if="
                    route.query.name === '1' ||
                    route.query.name === '2' ||
                    route.query.name === '6'
                  "
                >
                  <div class="title" v-if="form.keepCheck === '1'">
                    <h5>项目审批流程</h5>
                  </div>
                  <el-row>
                    <el-col :span="24">
                      <el-form-item label="审批方式">
                        <el-radio-group
                          v-model="formInline.processCheckBo.checkType"
                          :disabled="isDisabled()"
                        >
                          <el-radio :label="0">自由审批</el-radio>
                          <el-radio :label="1">逐级审批</el-radio>
                        </el-radio-group>
                      </el-form-item>
                    </el-col>

                    <el-col :span="24">
                      <el-form-item
                        label="审批流程"
                        style="padding: 20px 0"
                        v-if="
                          (route.query.name === '6' &&
                            formInline.processCheckBo.checkType === 0 &&
                            form.keepCheck === '1') ||
                          (route.query.name !== '6' && form.keepCheck === '1')
                        "
                      >
                        <approver
                          :approverList="
                            formInline.processCheckBo.checkPeopleIds
                          "
                          :typeId="3"
                          :isEdit="!!$route.query.projectId"
                          @handleChooseapprover="handleChooseapprover"
                        />
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item
                        label="抄送"
                        style="padding: 20px 0"
                        v-if="
                          (route.query.name === '6' &&
                            formInline.processCheckBo.checkType === 0 &&
                            form.keepCheck === '1') ||
                          (route.query.name !== '6' && form.keepCheck === '1')
                        "
                      >
                        <approver
                          :typeId="3"
                          :isCc="true"
                          :approverList="formInline.processCheckBo.copyFor"
                          @handleChooseapprover="handleChooseapproverCc"
                          style="background: #fff"
                        ></approver>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </div>
                <el-row
                  v-if="route.query.name === '6' || route.query.name === '5'"
                >
                  <el-col :span="24">
                    <el-form-item label="审批意见" prop="projectImplementation">
                      <el-input
                        class="input-item"
                        type="textarea"
                        :autosize="{ minRows: 2 }"
                        maxlength="150"
                        show-word-limit
                        v-model="formInline.approvalOpinions"
                        placeholder="请输入"
                        :disabled="route.query.name === '5' ? true : false"
                      ></el-input>
                    </el-form-item>
                    <el-form-item label="添加附件/图片" prop="approvalFiles">
                      <fileupload
                        @update:modelValue="approvalFilesAdd"
                        :modelValue="formInline.approvalFiles"
                      ></fileupload>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item
                      label="审批决定"
                      prop="projectImplementation"
                      v-if="formInline.processCheckBo.checkType !== 1"
                    >
                      <el-radio-group
                        v-model="form.keepCheck"
                        class="ml-4"
                        :disabled="route.query.name === '5' ? true : false"
                      >
                        <el-radio label="0" size="large">完成审批</el-radio>
                        <el-radio label="1" size="large">继续审批</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-col>
            </div>
          </el-col>
        </el-row>
        <div
          v-if="hasButtonSave"
          style="display: flex; justify-content: center; margin-top: 10px"
        >
          <el-button v-if="userStore.rolescode!=15" @click="resetForm(ruleFormRef)">{{
            route.query.name === "6" ? "驳回" : "取消"
          }}</el-button>
          <el-button v-if="userStore.rolescode!=15" type="primary" @click="submitForm(ruleFormRef)">{{
            route.query.name === "6" ? "通过" : "提交"
          }}</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>
<script setup>
import RouterHeader from "@/components/RouterHeader";
import Upload from "@/components/Upload";
import approver from "@/components/Approve/approver.vue";
import { useRouter, useRoute } from "vue-router";
import { onMounted } from "vue";
import fileupload from "@/components/FileUpload/index";
import indexcategory from "@/components/FileUpload/indexcategory";
import indexcategorysecond from "@/components/FileUpload/indexcategorysecond";
import { ElMessageBox, ElMessage } from "element-plus";
import {
  approvalQuery,
  divisionDown,
  industryDownApi,
} from "@/api/project/approval";
//@ts-ignore
import {
  archiveDetail,
  archiveUpdate,
  confirmArchive,
  addFiling,
} from "@/api/project/archive";

import {
  businessTypeList,
  location,
  conversionDetailsList,
  getbusinessTypeDic
} from "@/api/project/conversion";

import {
  approvalProcessApi,
  fileApprove,
} from "@/api/approvalManagement/approvaType.js";

import useUserStore from "@/store/modules/user";
const userStore = useUserStore();

const radio1 = ref("2");
const router = useRouter();
const route = useRoute();
const disabled = ref(false);
const activities = ref([]);

const title = () => {
  if (route.query.name === "1") {
    return "新增项目归档";
  } else if (route.query.name === "2") {
    return "编辑项目归档";
  } else {
    return "详情项目归档";
  }
};
// 2.控制所有表单项是否只读
const isDisabled = () => {
  if (
    route.query.name === "3" ||
    route.query.name === "5" ||
    route.query.name === "6"
  ) {
    return (disabled.value = true);
  } else {
    return (disabled.value = false);
  }
};
const hasButtonSave = ref(false);

// 1.按钮保存隐藏
const hasButton = () => {
  if (route.query.name === "3" || route.query.name === "5") {
    hasButtonSave.value = false;
  } else {
    hasButtonSave.value = true;
  }
};

const form = reactive({
  projectName: "", //项目名称
  enterpriseName: "", //企业名称
  enterprisePhone: "", //项目联系人电话
  enterprisePerson: "", //项目联系人
  industry: "", // 行业
  addressList: [], //所在地区
  projectProperty: "", //项目属性
  approvalDept: "", // 审批部门
  superviseDept: "", // 监督部门
  businessTypeList: [], // 业务类型
  secrecyAgreement: 0, //保密协议
  tenderProject: 0, // 投标项目
  projectBriefly: "", //项目简述
  projectRemark: "", //项目备注
  secrecyAgreementFile: "",
  secrecyAgreementFileList: [], //上传保密协议
  projectAnnex: "",
  projectAnnexList: [], //上传附件
  startDate: "", // 计划开始时间
  endDate: "", //计划结束时间
  priority: "", //  优先级
  keepCheck: "1", //审批决定-继续审批
});

const formInline = reactive({
  documentSupplementList: [],
  projectId: "",
  processCheckBo: {
    checkPeopleIds: [], //审核人id列表
    copyFor: [], //抄送人id列表
    checkType: 1,
  },
  taskList: [],
  approvalOpinions: "", // 审批意见
  processCheckHistoryId: "",
  getmoney: "",
  approvalFiles: [],
});
// 优先级option
const priorityOptions = [
  {
    value: "1",
    label: "最高",
  },
  {
    value: "2",
    label: "较高",
  },
  {
    value: "3",
    label: "较低",
  },
  {
    value: "4",
    label: "最低",
  },
];

// 全部地区配置
const areaCodeprops = {
  value: "id",
  label: "name",
  multiple: true,
  // checkStrictly:true,
  children: "rcDistrictList",
};
const businesstypeTree = ref([]);

// 改变业务类型
const businessChange = () => {};

// 业务类型下拉选项
const businessTypeOptions = async () => {
  let params = {};
  const response = await businessTypeList(params);
  if (response.code === 200 && response.data.length >= 0) {
    businesstypeTree.value = response.data;
  } else {
    ElMessage.error(res.msg);
  }
};
// 全部地区列表
const areaList = ref([]);

const getareaList = async () => {
  const response = await location();
  if (response.code === 200 && response.data) {
    areaList.value = response.data;
    
  } else {
    ElMessage.error(res.msg);
  }
};

// 业务类型props配置
const businesstProps = {
  value: "id",
  label: "businessName",
  children: "children",
  checkStrictly: true,
  multiple: true,
};

// 编辑 详情
let ids;
const checkArchive = async () => {
  const res = await archiveDetail(route.query.archivistId);
  // console.log(res, "---res-");

  if (res.code === 200 && res.data) {
    //文件信息
    formInline.taskList = res.data.taskList;
    // 文件转换
    formInline.documentSupplementList = res.data.documentSupplementList;
    formInline.processCheckHistoryId =
      res.data.dataCheckInfo.processCheckHistoryId;
    formInline.getmoney = res.data.getmoney;
    // 审批进度列表
    activities.value = res.data.dataCheckInfo && res.data.dataCheckInfo.opinons;
    // 审批方式
    formInline.processCheckBo.checkType =
      res.data && res.data.dataCheckInfo && res.data.dataCheckInfo.checkType;

    if (route.query.name === "2") {
      archivistId.value = res.data.archivistId;
      documentSupplement.value = res.data.documentSupplement;
    }
  } else {
    ElMessage.error(res.msg);
  }
  if (
    route.query.name === "2" ||
    route.query.name === "3" ||
    route.query.name === "6"
  ) {
    ids = res.data.projectId;
  }

  if (route.query.name === "5") {
    ids = res.data.projectId;
    formInline.approvalOpinions =
      res.data &&
      res.data.dataCheckInfo &&
      res.data.dataCheckInfo.myCheckOpinionBo &&
      res.data.dataCheckInfo.myCheckOpinionBo.opinion;
    formInline.approvalFiles =
      res.data &&
      res.data.dataCheckInfo &&
      res.data.dataCheckInfo.myCheckOpinionBo &&
      res.data.dataCheckInfo.myCheckOpinionBo.fileList;
    form.keepCheck =
      res.data &&
      res.data.dataCheckInfo &&
      res.data.dataCheckInfo.myCheckOpinionBo &&
      res.data.dataCheckInfo.myCheckOpinionBo.keepCheck;
  }
};
// 监督部门option
const superviseDeptOptions = ref([
  {
    id: "1",
    label: "住建",
  },
  {
    id: "2",
    label: "采购办",
  },
  {
    id: "3",
    label: "水利",
  },
  {
    id: "4",
    label: "交通",
  },
  {
    id: "5",
    label: "发改",
  },
  {
    id: "6",
    label: "环保",
  },
  {
    id: "7",
    label: "其他",
  },
]);
// 审批部门option
const approvalDeptOptions = ref([
  {
    id: "1",
    label: "生态环境局",
  },
  {
    id: "2",
    label: "行政审批局",
  },
  {
    id: "3",
    label: "经济发展局",
  },
  {
    id: "4",
    label: "发展改革局",
  },
  {
    id: "5",
    label: "其他",
  },
]);
const industryDownOption = ref([]);
// 获取行业下拉列表
const getindustryDown = async () => {
  const res = await industryDownApi();
  if (res.code === 200) {
    industryDownOption.value = res.data;
  }
};

let memberId = ref();
// 回显立项
const approvalQueryInfo = async () => {
  let params = {
    id:
      route.query.name === "2" ||
      route.query.name === "3" ||
      route.query.name === "5" ||
      route.query.name === "6"
        ? ids
        : Number(route.query.id),
  };

  const res = await approvalQuery(params);
  if (res.code === 200 && res.data) {
    form.projectName = res.data.projectName;
    form.enterpriseName = res.data.enterpriseName;
    form.enterprisePhone = res.data.enterprisePhone;
    form.enterprisePerson = res.data.enterprisePerson;
    form.industry = JSON.parse(res.data.industry);
    
    form.addressList = JSON.parse(res.data.address);
    form.projectProperty = res.data.projectProperty;
    form.approvalDept = JSON.parse(res.data.approvalDept);
    form.superviseDept = JSON.parse(res.data.superviseDept);
    
    //通过判断业务类型的，来显示上传文件框
    form.businessTypeList = res.data.test;
    ist.value = showFileUpload(res.data.test);

    form.secrecyAgreement = res.data.secrecyAgreement;
    form.secrecyAgreementFileList = res.data.secrecyFileList;
    form.tenderProject = res.data.tenderProject;
    form.priority = res.data.priority;
    form.projectBriefly = res.data.projectBriefly;
    form.projectRemark = res.data.projectRemark;
    form.projectAnnexList = res.data.projectFileList;
    form.archivingStatus = res.data.archivingStatus;
    memberId.value = res.data.memberId;
  }
};

// 改变业务类型
const ist = ref('3');
const showFileUpload=(e)=>{
  //类型1为'a-j',类型2为'册'
  //返回值示例：
  // [
  //   {
  //       "type": "6,89",
  //       "value": 1
  //   },
  //   {
  //       "type": "[6,88],[6,90]",
  //       "value": 2
  //   }
  // ]

  //先判断类型1，再判断类型2，其它为3
  let typedic1 = businessTypeDicData.filter(o=>{ if(o.value == 1) return o } );
  let typedic2 = businessTypeDicData.filter(o=>{ if(o.value == 2) return o } );
  let result = '3';
  //进行业务类型的判断
  if(e.length == 0){
    return result;
  }
  e.forEach(function(item){
    typedic1.filter(function(o){
      let typearr = JSON.parse("["+ o.type +"]");
      typearr.forEach(function(p){
        if(p.length == 2 
        && p.length == item.length 
        && p[0] == item[0]
        && p[1] == item[1]){
          result = typedic1[0].value;
        }
        else if(p.length == 1 
        && p.length == item.length 
        && p[0] == item[0]){
          result = typedic1[0].value;
        }
      });
    });
  });

  if(result == '1'){
    return result;
  }
  e.forEach(function(item){
    typedic2.filter(function(o){
      let typearr = JSON.parse("["+ o.type +"]");
      typearr.forEach(function(p){
        if(p.length == 2 
        && p.length == item.length 
        && p[0] == item[0]
        && p[1] == item[1]){
          result = typedic2[0].value;
        }
        else if(p.length == 1 
        && p.length == item.length 
        && p[0] == item[0]){
          result = typedic2[0].value;
        }
      });
    });
  });

  return result;
}



// 首次归档 新建 默认带数据
const addArchive = async () => {
  let params = {
    proId: Number(route.query.id ? route.query.id : route.query.planId),
  };
  const res = await confirmArchive(params);
  if (res.code === 200 && res.data) {
    formInline.taskList = res.data;
    formInline.projectId = res.data.projectId;
  } else {
    ElMessage.error(res.msg);
  }
};

let archivistId = ref("");
let documentSupplement = ref("");

// 上传附件
const handleModelValueUpdate = (val, result, operationType) => {
  if (operationType === "success") {
    if (result) {
      formInline.documentSupplementList = [];
      result.forEach((item)=>{
        formInline.documentSupplementList.push(item);
      });
    }
  } else if (operationType === "delete") {
    formInline.documentSupplementList.splice(result, 1);
  }
};
// 审批附件
const approvalFilesAdd = (val, result, operationType) => {
  if (operationType === "success") {
    if (result) {
      formInline.approvalFiles = [];
      result.forEach((item)=>{
        formInline.approvalFiles.push(item);
      });
    }
  } else if (operationType === "delete") {
    formInline.approvalFiles.splice(result, 1);
  }
};

// 抄送人
const handleChooseapproverCc = (id) => {
  if (id) {
    formInline.processCheckBo.copyFor = id;
  }
};

// 审批人
const handleChooseapprover = (id) => {
  if (id) {
    formInline.processCheckBo.checkPeopleIds = id;
  }
};

// 添加
const submitForm = async () => {
  if (route.query.name === "1") {
    let newArray = [];
    let userCcIds;
    let userIds;
    let requestPayload;
    // 编辑默认回显的时候 审批人
    if (formInline.processCheckBo.checkPeopleIds) {
      userIds = formInline.processCheckBo.checkPeopleIds.map((item) =>
        item.userId ? Number(item.userId) : item
      );
    }
    if (formInline.processCheckBo.copyFor) {
      userCcIds = formInline.processCheckBo.copyFor.map((item) =>
        item.userId ? Number(item.userId) : item
      );
    }

    if (formInline.processCheckBo.checkType === 1) {
      // 逐级审批 orderCheckPeopleInfoList
      // 使用 map 方法重构数组
      newArray = formInline.processCheckBo.checkPeopleIds.map((item, index) => {
        return {
          orderNum: index + 1,
          userId: item.userId ? Number(item?.userId) : item,
        };
      });
      requestPayload = {
        ...formInline,
        proId: Number(route.query.proId),
        projectName: route.query.projectName,
        memberId: memberId.value,
        processCheckBo: {
          orderCheckPeopleInfoList: newArray, // 直接作为顶层属性
          copyFor: userCcIds, // 直接作为顶层属性
          checkType: formInline.processCheckBo.checkType,
        },
        archivingStatus : form.archivingStatus
      };
    } else {
      requestPayload = {
        ...formInline,
        proId: Number(route.query.proId),
        projectName: route.query.projectName,
        memberId: memberId.value,
        processCheckBo: {
          checkPeopleIds: userIds, // 设置新的 checkPeopleIds 属性值
          copyFor: userCcIds,
          checkType: formInline.processCheckBo.checkType,
        },
        archivingStatus : form.archivingStatus
      };
    }

    const response = await addFiling(requestPayload);
    if (response.code === 200) {
      // 添加成功的提示
      ElMessage.success(response.msg);
      // 跳转到列表
      router.push({
        path: "/project/archive",
      });
    } else {
      ElMessage.error(response.msg);
    }
  } else if (route.query.name === "2") {
    // 编辑默认回显的时候 审批人
    let newArray = [];
    let userCcIds;
    let userIds;
    let requestPayload;
    // 编辑默认回显的时候 审批人
    if (formInline.processCheckBo.checkPeopleIds) {
      userIds = formInline.processCheckBo.checkPeopleIds.map((item) =>
        item.userId ? Number(item.userId) : item
      );
    }
    if (formInline.processCheckBo.copyFor) {
      userCcIds = formInline.processCheckBo.copyFor.map((item) =>
        item.userId ? Number(item.userId) : item
      );
    }

    if (formInline.processCheckBo.checkType === 1) {
      // 逐级审批 orderCheckPeopleInfoList
      // 使用 map 方法重构数组
      newArray = formInline.processCheckBo.checkPeopleIds.map((item, index) => {
        return {
          orderNum: index + 1,
          userId: item.userId ? Number(item?.userId) : item,
        };
      });
      requestPayload = {
        ...formInline,
        archivistId: archivistId.value,
        documentSupplement: documentSupplement.value,
        processCheckBo: {
          orderCheckPeopleInfoList: newArray, // 直接作为顶层属性
          copyFor: userCcIds, // 直接作为顶层属性
          checkType: formInline.processCheckBo.checkType,
        },
      };
    } else {
      requestPayload = {
        ...formInline,
        archivistId: archivistId.value,
        documentSupplement: documentSupplement.value,
        processCheckBo: {
          checkPeopleIds: userIds, // 设置新的 checkPeopleIds 属性值
          copyFor: userCcIds,
          checkType: formInline.processCheckBo.checkType,
        },
      };
    }

    // 编辑
    const response = await archiveUpdate(requestPayload);
    if (response.code === 200) {
      // 添加成功的提示
      ElMessage.success(response.msg);
      // 跳转到列表
      router.push({
        path: "/project/archive",
      });
    } else {
      ElMessage.error(response.msg);
    }
  } else if (route.query.name === "6") {
    let data = {
      checkOpinion: formInline.approvalOpinions,
      id: formInline.processCheckHistoryId,
      copyFor: formInline.processCheckBo.copyFor.map((item) => item.userId),
      keepCheckId: formInline.processCheckBo.checkPeopleIds.map(
        (item) => item.userId
      ),
      state: "1",
      fileList: formInline.approvalFiles,
      keepCheck: form.keepCheck,
    };
    const res = await fileApprove(data);
    if (res.code === 200) {
      ElMessage.success("审批通过");
      router.push({
        path: "/examine",
      });
    }
  }
};

// 查看附件
const handleDetailFile = (val) => {
  console.log();
  router.push({
    path: "/project/fileStorageRetrieval",
    query: {
      proId: val.proId,
      projectName: val.projectName,
    },
  });
};

// 取消
const resetForm = async () => {
  if (route.query.name === "6") {
    let data = {
      checkOpinion: formInline.approvalOpinions,
      id: formInline.processCheckHistoryId,
      copyFor: formInline.processCheckBo.copyFor.map((item) => item.userId),
      keepCheckId: formInline.processCheckBo.checkPeopleIds.map(
        (item) => item.userId
      ),
      state: "0",
      fileList: formInline.approvalFiles,
    };
    const res = await fileApprove(data);
    if (res.code === 200) {
      ElMessage.success("审批已驳回");
      router.go(-1);
    }
  }
  if (route.query.projectName) {
    // 返回上一步
    router.back();
  } else {
    router.push({
      path: "/project/archive",
    });
  }
};

let businessTypeDicData = [];
//获取
const getbusinessTypeDicData = async() => {
  //类型1为'a-j',类型2为'册'
  //返回值示例：
  // [
  //   {
  //       "type": "6,89",
  //       "value": 1
  //   },
  //   {
  //       "type": "[6,88],[6,90]",
  //       "value": 2
  //   }
  // ]
  let response = await getbusinessTypeDic();
  if (response.code === 200 && response.rows.length > 0) {
    businessTypeDicData = response.rows;
  } else {
    console.log(err);
  }

}

onMounted(async () => {
  hasButton();
  //需要查询：需要文件分类的业务类型的数据
  await getbusinessTypeDicData();
  await getareaList();
  if (route.query.name === "1") {
    await addArchive();

    // getDivision();
  } else {
    await checkArchive();
  }
  
  await approvalQueryInfo();
  await getindustryDown();
  await businessTypeOptions();
  
  
});
</script>

<!-- <style>
.deep-input {
  border: 1px solid #409eff;
  border-radius: 30px;
  width: 300px;
}
.input-class {
  display: flex;
  justify-content: center;
  align-items: center;
}
.input-class .el-input-group__prepend {
  border-radius: 30px;
  border: 0;
  box-shadow: 0 0 0 0px;
}

.input-class .el-input-group__prepend {
  border-radius: 30px;
  border: 0;
  box-shadow: 0 0 0 0px;
}
.input-class .el-input__inner {
  text-align: center;
}
.input-class .el-input__wrapper {
  border-radius: 30px;
  border: 0;
  box-shadow: 0 0 0 0px;
}

.input-class .el-input-group__append {
  border-radius: 30px;
  border: 0;
  box-shadow: 0 0 0 0px;
}
</style> -->
<style lang="scss" scoped>
.title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  h5 {
    font-weight: bold;
  }
}
.fabu {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
.form-single-info {
  display: flex;
  justify-content: center;
}
</style>
